package com.ycz.springcloud.controller;/*
 @author ycz
 @date 2021-08-19-13:51  
*/

import com.ycz.springcloud.common.Constant;
import com.ycz.springcloud.common.Result;
import com.ycz.springcloud.form.SysUserForm;
import com.ycz.springcloud.form.SysUserResetPwdForm;
import com.ycz.springcloud.query.SysUserQuery;
import com.ycz.springcloud.service.SysUserService;
import com.ycz.springcloud.vo.SysUserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("page.do")
   // @HystrixCommand(fallbackMethod = "hystrixPage")
    public Object page(SysUserQuery query){
        return sysUserService.queryPage(query);
    }

    public Object hystrixPage(SysUserQuery query){
        return "不存在改数据库，或已经熔断";
    }

    @RequestMapping("page1.do")
//    @HystrixCommand(fallbackMethod = "hystrixPage")
    public Map<String,Object> list(@RequestBody SysUserQuery query){
        /**
         *
         * 单纯传一个List数值过去了
         */
        System.out.println("第"+query.getPage()+"页");
        System.out.println("limit"+query.getLimit());
//        Result result = new Result();
        /**
         * 内部的xml文件的page，and，limit
         */
        Map<String,Object> map=new HashMap<>();
//        query.setPage(page);
//        query.setLimit(limit);
//        result.setData(sysUserService.selectList2(query));
        map.put("data",sysUserService.selectList2(query));
        map.put("count",sysUserService.getUserCount(query));
        return map;
    }

    @RequestMapping("/export2.do")
    public List<SysUserVo> export2(SysUserQuery query){
        return sysUserService.selectList(query);
    }

    /**
     * 添加用户
     * @param form
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody SysUserForm form){
        System.out.println(form);
        return sysUserService.add(form);
    }

    /**
     * 登录
     * @param loginName 用户名
     * @param loginPassword 密码
     * @return
     */
    @PostMapping("/login.do")
    public Map<String,Object> login( String loginName, String loginPassword){
        //import org.apache.shiro.subject.Subject; import org.apache.shiro.SecurityUtils;
        String username=loginName;
        Md5Hash md5Hash = new Md5Hash(loginPassword, Constant.MD5_SALT, 2);
        System.out.println(md5Hash.toString());
        String password=md5Hash.toString();
        Subject subject= SecurityUtils.getSubject();
        //将用户名与密码放进去
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        Map<String,Object> map=new HashMap<>();
        Session session=subject.getSession();
        try {
            subject.login(token);
            map.put("code",200);
            map.put("msg","操作成功");
            map.put("data","");
            map.put("loginName",loginName);
            map.put("role",session.getAttribute("role"));
            return map;
        }catch (UnknownAccountException e) {//用户名错误)
            map.put("code",110);
            map.put("msg","用户名或密码错误");
            map.put("data","");
            return map;

        }catch (IncorrectCredentialsException e){
            map.put("code",110);
            map.put("msg","用户名或密码错误");
            map.put("data","");
            return map;
        }
    }

    public String noauth(Model model){
        return "未经授权不能进入";
    }

    @RequestMapping("/export.do")
    public Result exportUser(SysUserQuery query, HttpServletResponse response) throws IOException {
        System.out.println("要导出的数据"+query);
        sysUserService.exportUser(query,response);
        return new Result();
    }

//    @GetMapping("/export.do")
//    public Result exportUser2(SysUserQuery query, HttpServletResponse response) throws IOException {
//        System.out.println("要导出的数据"+query);
//        sysUserService.exportUser(query,response);
//        return new Result();
//    }

    @RequestMapping("/import.do")
    public Result result( @RequestBody List<SysUserVo> sysUserVos){
        System.out.println(sysUserVos);
        sysUserService.batchInsert(sysUserVos);
        return new Result();
    }

    @RequestMapping("/delete.do")
    public Result delete(@RequestBody List<Integer> ids){
        System.out.println("要删除的"+ids);
        sysUserService.deleteUser(ids);
        return new Result();
    }

    @PostMapping("/updatePassword.do")
    public Result updatePassword(@RequestBody SysUserResetPwdForm form) {
        System.out.println("原来的密码"+form.getUsedPassword());
        System.out.println("新密码"+form.getPassword());
        System.out.println("再次输入的密码"+form.getNewPassword());


        return sysUserService.updatePassword(form);
    }


}
